package dao;

import common.Answer;
import common.Homework;
import service.FlushCourse;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CorrectHW {
    private static Connection conn=DataBaseConnect.getConn();
    private static String sql1="update homework set score=? where id=?";
    private static String sql2="update answer set score=? where id=?";
    private static PreparedStatement pstmt1,pstmt2;
    static {
        try {
            pstmt1=conn.prepareStatement(sql1);
            pstmt2=conn.prepareStatement(sql2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void correctHW(Homework homework){
        try {
            pstmt1.setString(1, homework.getScore());
            pstmt1.setString(2, homework.getId());
            pstmt1.executeUpdate();
            for(Answer answer:homework.getAnswers()){
                pstmt2.setString(1, answer.getScore());
                pstmt2.setString(2, answer.getId());
                pstmt2.executeUpdate();
            }
            FlushCourse.flush(homework.getStudentid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
